﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Text;
using log4net;
using log4net.Config;
using System.IO;
using Crawler;

namespace PreguntaAlExperto
{
    public class Global : System.Web.HttpApplication
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Global));

        protected void Application_Start(object sender, EventArgs e)
        {
            logger.Info("Se ha inicialdo la aplicación");

#if SEARCHENABLED
            //new Crawler.Crawler().Crawl();  
            //Catalog.Load();
#endif
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            Exception objException = Server.GetLastError();

            for (; objException != null; objException = objException.InnerException)
            {
                builder.Append(string.Format(
                        "\nAtrapado en Application_Error\n" +
                        "Exception      : {0}\n" +
                        "Exception in   : {1}\n" +
                        "RawUrl         : {2}\n" +
                        "UrlReferrer    : {3}\n" +
                        "UserHostAddress: {4}\n" +
                        "UserHostName   : {5}\n" +
                        "Error Message  : {6}\n" +
                        "Stack Trace    : {7}",
                        objException.GetType().FullName,
                        Request.Url.ToString(),
                        Request.RawUrl,
                        Request.UrlReferrer,
                        Request.UserHostAddress,
                        Request.UserHostName,
                        objException.Message.ToString(),
                        objException.StackTrace.ToString()
                        ));

            }
            logger.Error(builder.ToString());
            Server.ClearError();
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}